//
//  ViewController.m
//  KAi 眼
//
//  Created by xalo on 16/7/1.
//  Copyright © 2016年 朱旭东. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
 /****************************************************
                                    rilegoule
  ********************************************************/
#if 0
- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray index:(NSInteger)index{
        
        self = [super initWithFrame:frame];
        
        if (self) {
            
            self.backgroundColor = [UIColor clearColor];
            self.userInteractionEnabled = YES;
            
            self.contentMode = UIViewContentModeTop;
            self.clipsToBounds = YES;
            _scrollView = [[ContentScrollView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight - 64) imageArray:imageArray index:index];
            [self addSubview:_scrollView];
            
            self.scrollView.userInteractionEnabled = YES;
            //
            InterfaceModel *model = imageArray[index];
            //
            _contentView = [[ContentView alloc]initWithFrame:CGRectMake(0, kHeight / 1.7, kWidth, kHeight - kHeight / 1.7) Width:35 model:model collor:[UIColor whiteColor]];
            [_contentView setData:model];
            [self addSubview:_contentView];
            //
            _playView = [[UIImageView alloc]initWithFrame:CGRectMake((kWidth - 100) / 2, (kHeight/1.7 - 100) / 2 + 64, 100, 100)];
            _playView.image = [UIImage imageNamed:@"video-play"];
            
            [self addSubview:_playView];
            
            _animationView = [[InterfaceTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:nil];
            [_animationView.coverview removeFromSuperview];
            
            [self addSubview:_animationView];
            
            _playView.alpha = 0;
            
            _scrollView.alpha = 0;
        }
        return self;
    }
    
    //播放动画
    - (void)aminmationShow{
        
        self.contentView.frame = CGRectMake(0, self.offsetY, kWidth, 250);
        self.animationView.frame = CGRectMake(0, self.offsetY, kWidth, 250);
        self.animationView.picture.transform = self.animationTrans;
        
        [UIView animateWithDuration:0.5 animations:^{
            
            self.animationView.frame = CGRectMake(0, 64, kWidth, kHeight / 1.7);
            self.animationView.picture.transform = CGAffineTransformMakeTranslation(0,  (kHeight / 1.7 - 250)/2);
            
            self.contentView.frame = CGRectMake(0, kHeight / 1.7 + 64, kWidth, kHeight - kHeight / 1.7 -64);
        } completion:^(BOOL finished) {
            
            self.scrollView.alpha = 1;
            
            [UIView animateWithDuration:0.25 animations:^{
                
                self.animationView.alpha = 0;
                self.playView.alpha = 1;
                
            } completion:^(BOOL finished) {
                
            }];
        }];
        
        
    }
    
    //动画消失时
    - (void)animationDismissUsingCompeteBlock:(void (^)(void))complete {
        
        [UIView animateWithDuration:0.25 animations:^{
            
            self.animationView.alpha = 1;
        } completion:^(BOOL finished) {
            
            self.scrollView.alpha = 0;
            self.playView.alpha = 0;
            [UIView animateWithDuration:0.5 animations:^{
                
                CGRect rec = self.animationView.frame;
                rec.origin.y = self.offsetY;
                rec.size.height = 250;
                self.animationView.frame = rec;
                self.animationView.picture.transform = self.animationTrans;
                self.contentView.frame = rec;
                
            } completion:^(BOOL finished) {
                
                self.animationTrans = CGAffineTransformIdentity;
                
                [self.contentView removeFromSuperview];
                
                [UIView animateWithDuration:0.25 animations:^{
                    self.animationView.alpha = 0;
                } completion:^(BOOL finished) {
                    [self removeFromSuperview];
                    
                    complete();
                }];
                
            }];
        }];
        
    
    }
#endif

/*************************************************************
                           BlurImageView
***************************************************************/
#if 0
- (instancetype)initWithFrame:(CGRect)frame Width:(CGFloat)width model:(InterfaceModel *)model collor:(UIColor *)collor{
        
        self = [super initWithFrame:frame];
        
        if (self) {
            
            self.clipsToBounds = YES;
            
            _picture = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight / 1.7)];
            _picture.contentMode = UIViewContentModeScaleAspectFill;
            
            [self addSubview:_picture];
            
        }
        return self;
    }
    
    - (void)imageOffset {
        
        CGRect centerToWindow = [self convertRect:self.bounds toView:nil];
        
        CGFloat centerX = CGRectGetMidX(centerToWindow);
        CGPoint windowCenter = self.window.center;
        
        CGFloat cellOffsetX = centerX - windowCenter.x;
        
        CGFloat offsetDig =  cellOffsetX / self.window.frame.size.height *2;
        
        CGAffineTransform transX = CGAffineTransformMakeTranslation(- offsetDig * kWidth * 0.7, 0);
        
        
        self.picture.transform = transX;
    }

    
#endif
    
/*************************************************************
                                ImageContentView
***************************************************************/
#if 0
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.userInteractionEnabled = YES;
            //设置图片
            self.image = [UIImage imageNamed:@"11471923,2560,1600.jpg"];
            //创建模糊视图
            UIVisualEffectView *backVisual = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
            //将模糊视图的大小等同于自身
            backVisual.frame = self.bounds;
            //设置模糊视图的透明度
            backVisual.alpha = 1;
            [self addSubview:backVisual];
            
        }
        return self;
    }

#endif
/*************************************************************
                                  ContentVIew
***************************************************************/
#if 0
- (instancetype)initWithFrame:(CGRect)frame Width:(CGFloat)width model:(InterfaceModel *)model collor:(UIColor *)collor{
        
        self = [super initWithFrame:frame];
        
        if (self) {
            self.contentMode = UIViewContentModeScaleAspectFill;
            self.clipsToBounds = YES;
            
            _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
            
            [self addSubview:_imageView];
            _imageView.contentMode = UIViewContentModeScaleAspectFill;
            
            _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, kWidth, 30)];
            _titleLabel.font = [UIFont boldSystemFontOfSize:16];
            _titleLabel.textColor = collor;
            [self addSubview:_titleLabel];
            
            _lineView = [[UIView alloc]initWithFrame:CGRectMake(5, 35, 200, 1)];
            _lineView.backgroundColor = collor;
            [self addSubview:_lineView];
            
            _littleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 46, kWidth, 20)];
            _littleLabel.textColor = collor;
            _littleLabel.font = [UIFont systemFontOfSize:14];
            //        _littleLabel.backgroundColor = [UIColor redColor];
            [self addSubview:_littleLabel];
            
            _descripLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 80, kWidth - 10, 90)];
            _descripLabel.font = [UIFont systemFontOfSize:14];
            //        _descripLabel.backgroundColor = [UIColor redColor];
            _descripLabel.numberOfLines = 0;
            _descripLabel.textColor = collor;
            [self addSubview:_descripLabel];
            
            CGFloat y = _descripLabel.frame.size.height + 90;
            _collectionCustom = [[CustomView alloc]initWithFrame:CGRectMake(5, y, (kWidth - 10) / 4, 30) Width:width LabelString:model.collectionCount collor:collor];
            [self addSubview:_collectionCustom];
            
            _shareCustom = [[CustomView alloc]initWithFrame:CGRectMake((kWidth - 10) / 4 + 5, y, (kWidth - 10) / 4, 30) Width:width LabelString:model.shareCount collor:collor];
            [self addSubview:_shareCustom];
            _cacheCustom = [[CustomView alloc]initWithFrame:CGRectMake(_shareCustom.frame.origin.x + _shareCustom.frame.size.width, y, (kWidth - 10) / 4, 30) Width:width LabelString:@"缓存" collor:collor];
            [self addSubview:_cacheCustom];
            _replyCustom = [[CustomView alloc]initWithFrame:CGRectMake(_cacheCustom.frame.origin.x + _cacheCustom.frame.size.width, y, (kWidth - 10) / 4, 30) Width:width LabelString:model.replyCount collor:collor];
            [self addSubview:_replyCustom];
            
            [_imageView sd_setImageWithURL:[NSURL URLWithString:model.coverForDetail]];
            
            [self setData:model];
        }
        return self;
    }
    
- (void)setData:(InterfaceModel *)model {
        
        self.titleLabel.text = model.title;
        self.descripLabel.text = model.descrip;
        [self.shareCustom setTitle:model.shareCount];
        [self.replyCustom setTitle:model.replyCount];
        [self.collectionCustom setTitle:model.collectionCount];
        
        NSInteger time = [model.duration integerValue];
        NSString *timeString = [NSString stringWithFormat:@"%02ld'%02ld''",time/60,time% 60];//显示的是音乐的总时间
        NSString *string = [NSString stringWithFormat:@"#%@ / %@",model.category, timeString];
        self.littleLabel.text = string;
        
        
        __weak typeof(self) weakSelf = self;
        
        
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:model.coverBlurred] options:(SDWebImageRetryFailed) progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            
            if (image) {
                
                CABasicAnimation *contentsAnimation = [CABasicAnimation animationWithKeyPath:@"contents"];
                contentsAnimation.duration = 1.0f;
                contentsAnimation.fromValue = self.imageView.image ;
                contentsAnimation.toValue = image;
                
                contentsAnimation.removedOnCompletion = YES;
                contentsAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                [weakSelf.imageView.layer addAnimation:contentsAnimation forKey:nil];
                
                weakSelf.imageView.image = image;
                
            }
            
        }];
    }

#endif
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
